<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
/**
 * 列表页管理控制器
 * @author 武斌   <wubin.mail@foxmail.com>
 */
class ListController extends CommonController{
	protected $db;
	protected $goods;
	protected $goodsAttr;
	protected $typeAttr;
	//构造函数
	public function __init()
	{
		$this->db = new \Home\Model\Cate;
		$this->goods = new \Home\Model\Goods;
		$this->goodsAttr = new \Home\Model\GoodsAttr;
		$this->typeAttr  = new \Home\Model\TypeAttr;
	}
	
    //动作
    public function index(){
		//公共头部注入变量
		$pageConf = array(
			'css'=>'list',
			'js'=>'list',
			'title'=>'商品列表',
		);  
		View::with('pageConf',$pageConf);
		//get参数，分类id
    	$cid = Q('get.cid',0,'intaval');
    	//面包屑处理
    	//获取所有分类数据
    	$cateData = $this->db->get();
		//调用CommonController中的getFather(全部分类数据,查找的父级数据)
		$fatherData = $this->getFather($cateData,$cid);
		//将数组进行反转，将父级放到最前变
		$fatherData = array_reverse($fatherData);
		View::with('fatherData',$fatherData);
		//左侧分类处理
		$allCateData = Data::channelLevel($cateData, $pid = 0, $html = "&nbsp;", $fieldPri = 'cid', $fieldPid = 'pid', $level = 1);
		View::with('allCateData',$allCateData);
		//筛选
		//1.当前cid以及其全部子集集合
		$cids = $this->getSon($cid);
		//在商品表中查询到属于这些分类的全部商品
		$gids = $this->goods->where("category_cid in ( " . implode(',', $cids) . ")")->lists('gid');
		if($gids)
		{
			//根据商品gid得到这些商品都有哪些属性，会出现有重复的，根据gavalue进行分组
			$goodsAttr = $this->goodsAttr->where("goods_gid in ( " . implode(',', $gids) . ")")->groupBy('gavalue')->get();
			
			//以下两个临时数组为重组列表页需要的属性
			//将类型属性id是一样的放在同一个数组中
			$tempAttr = array();
			foreach ($goodsAttr as $k => $v) {
				$tempAttr[$v['typeAttr_taid']][] = $v;
			}
			//组合属性
			$tempFinalAttr = array();
			foreach ($tempAttr as $k => $v) {
				$tempFinalAttr[] = array(
					//属性名称
					'name' => $this->typeAttr->where('taid',$k)->pluck('taname'),
					//属性值
					'value'=>$v,
				);
			}
			View::with('tempFinalAttr',$tempFinalAttr);	
			//进行筛选处理
			$num = count($tempFinalAttr);//最终属性的总数
			//设置param参数，用0填充
			$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
			View::with('param',$param);
			foreach ($param as $k => $v) {
				//$v是类型属性id//筛选出来的gid
				//如果有筛选的时候，就是param参数不为零的进行处理
				if($v)
				{	//自关联获得当前商品属性包含的商品，上边用了groupBy进行了分组，防止有漏掉的，采用自关联获得筛选的商品id集合
					$filterGids[][] = Db::table('goodsattr ga1')->join('goodsattr ga2','ga1.gavalue','=','ga2.gavalue')
									->where('ga1.gaid',$v)->pluck('goods_gid');
				}
			}
			//存在商品id集合
			if(isset($filterGids))
			{	//拿到商品id集合的第一条，遍历数组求交集
				$finalGids = $filterGids[0];
				foreach ($filterGids as $k => $v) {
					$finalGids = array_intersect($finalGids, $v);
				}
				//最后的结果再跟分类得到的商品id求交集
				$finalGids = array_intersect($finalGids,$gids);
			}else
			{	
				$finalGids = $gids;
			}
		}else
		{	//分类下边没有商品，最后gid集合为空数组
			$finalGids = array();
		}
		//如果最后商品id集合不为空数组的时候
		if(!empty($finalGids))
		{	//分页数据
			$page = Page::row(30)->make($this->goods->where("gid in ( " . implode(',', $finalGids) . ")")->count());
			//书友数据
			$data = $this->goods->where("gid in ( " . implode(',', $finalGids) . ")")->limit(Page::limit())->get();
			View::with('page',$page);
			View::with('goodsData',$data);
		}
       View::make(TEMPLATE . '/list.html');
    }
	/**
	 * 把自己追加到子集中去
	 */
	public function getSon($cid)
	{	//分类全部数据
		$data = $this->db->get();
		//调用当前控制器getSonCid方法，获得子集
		$cids = $this->getSonCid($data,$cid);
		//把自己追加到子集中
		$cids[] = $cid;
		//将自己和子集集合返出
		return $cids;
	}
	/**
	 * 找子集
	 */
	public function getSonCid($data,$cid)
	{	//递归给改数组一直追加，使用静态变量，为了不丢失之前的数据
		$temp = array();
		foreach ($data as $k => $v) {
			if($v['pid'] == $cid)
			{
				$temp[] = $v['cid'];
				//$this->getSonCid($data,$v['cid']);
				$temp = array_merge($temp,$this->getSonCid($data,$v['cid']));
			}
		}
		p($temp);
		return $temp;
	}

}